﻿using FTchinaMVC.Models;
using MVCExtension.FrameWork;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FTchinaMVC.Controllers
{
    public class ManageController : Controller
    {
        private FTchinaMVCContext db = new FTchinaMVCContext();

        //
        // GET: /Manage/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取导航菜单
        /// </summary>
        /// <returns></returns>
        public ActionResult GetMenu()
        {
            string tmpWords = string.Empty;
            string isParent = "false";//是否父菜单，Ztree用
            string url = string.Empty;//点击跳转地址
            string format = " id: {0}, pId: {1}, name: '{2}',isParent: {3}, target: 'mainFrame' ";
            var Menus = db.MenuModels.ToList<Menu>();
            tmpWords = "[";
            foreach (Menu mu in Menus)
            {

                if (Menus.Where(m => m.ParentID == mu.ID).Count<Menu>() > 0)
                {
                    isParent = "true";
                    url = "";
                }
                else
                {
                    isParent = "false";
                    url = ",url: '/" + mu.Controller + "/'";
                }
                tmpWords += "{" + string.Format(format, mu.ID, mu.ParentID, mu.MenuName, isParent) + url + "},";
            }

            if (tmpWords.EndsWith(","))
            {
                tmpWords = tmpWords.Substring(0, tmpWords.Length - 1);
            }
            tmpWords += "]";
           // Logger.Info(tmpWords);
            return Content(tmpWords);
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
